#!/usr/bin/python


import os
import sys
import shutil
from datetime import datetime,date
import ConfigParser

def runCommand(cmd):
	#print cmd
	if sys.version_info[0] < 3:
		import commands	
	else:
		import subprocess

	if sys.version_info[0] < 3:
		ret = commands.getstatusoutput(cmd)
	else:
		ret = subprocess.getstatusoutput(cmd)

	if not ret[0] == 0:
		print ("Command : " + cmd)
		print (ret[1])

	return ret[1]

class Settings:
	def __init__(self):
		self.backup_path = "/tmp/backup_tmp"
		self.initConfig()

	def initConfig(self):	
		self.config = ConfigParser.ConfigParser()
		conf_file = "/etc/backup.cfg"
		self.config.read(conf_file)
		if not self.config.has_section("Settings"):
			self.config.add_section("Settings")

		if not self.config.has_option("Settings","tmppath"):
			self.config.set("Settings","TmpPath",self.backup_path)
		self.backup_path = self.config.get("Settings","tmppath")

		if not self.config.has_option("Settings","server"):
			self.config.set("Settings","server","")
		self.backup_server = self.config.get("Settings","server")

		if not self.config.has_option("Settings","user"):
			self.config.set("Settings","user","")
		self.backup_user = self.config.get("Settings","user")

		if not self.config.has_option("Settings","pass"):
			self.config.set("Settings","pass","")
		self.backup_pass = self.config.get("Settings","pass")
		
		print("Init Config")

		runCommand("mkdir -p " + self.backup_path)
	
		self.config.write(open(conf_file,"w"))

	def getBackups(self):
		secs = self.config.sections()

		backups = []
		for a in secs:
			if not a ==	"Settings":
				backups.append(a)

		return backups

	def getBackupPath(self,name):
		return self.config.get(name,"path")


def rsync_backup(name):
	print( "\033[00;32m * \033[00mCalc " + name)
	cmd = "du -h " + str(settings.getBackupPath(name)) + " --summarize | cut -f 1"
	size = runCommand(cmd)
	print( "\033[00;32m * \033[00mCopy " + name  + " " + size)
	path = settings.getBackupPath(name)
	cmd = "rsync -ra --delete --delay-updates --force " + path + " " + settings.backup_path + "/" + name  
#	print cmd
	runCommand(cmd)

	back_dir = settings.backup_path + "/" + name
	snar_file = back_dir + ".snar"
	if not os.path.exists(snar_file):
		print( "\033[00;32m * \033[00mPack " + name + " full")
		archiv = back_dir + "_" + str(date.today()).replace(" ","_") +  "_full.tar.xz"
		cmd = "cd " + back_dir + " ; tar --force-local --listed-incremental=" + snar_file + " -cS * | xz -9 > " + archiv 
	else:
		print( "\033[00;32m * \033[00mPack " + name + " inc")
		archiv = back_dir + "_" + str(date.today()).replace(" ","_") +  "_inc.tar.xz"
		shutil.copyfile(snar_file,snar_file+".1")
		cmd = "cd " + back_dir + " ; tar --force-local --listed-incremental=" + snar_file + ".1 -cS * | xz -9 > " + archiv 
#	print cmd
	runCommand(cmd)

	print( "\033[00;32m * \033[00mscp  " + name )
	cmd = "scp " + archiv + " " + settings.backup_user + "@" + settings.backup_server + ":"
#	print cmd
	runCommand(cmd)

settings = Settings()
backups = settings.getBackups()


for a in backups:
	rsync_backup(a)







